Option Explicit

Implements ISql
Implements IExpression
Implements ISqlTableDefine
Implements ISubSelectContainer

'################################################################
' Field
'################################################################
' 当前查询结果表的临时名
Private tempName As String

' 表字段集合
' List<SelectItem>
Private fieldSelectItems As Collection

' 表
' 包括 From/Join
' List<IFromItem>
Private froms As Collection

' where 条件的封装
Private where As SqlWhere

' 分组条件的封装
' List<GroupbyItem>
Private groupbys As Collection

' 排序条件的封装
' List<OrderbyItem>
Private orderbys As Collection

'################################################################
' Constructor
'################################################################
Private Sub Class_Initialize()
    Set fieldSelectItems = new Collection
    Set froms = new Collection
    Set where = new SqlWhere
    Set groupbys = new Collection
    Set orderbys = new Collection

    ' 对于一个查询/子查询，没有真实的表名，在这里生成一个临时表名
    tempName = UtilsRandom.enNumStr(20)
End Sub

Private Sub Class_Terminate()
    Set fieldSelectItems = Nothing
    Set froms = Nothing
    Set where = Nothing
    Set groupbys = Nothing
    Set orderbys = Nothing
End Sub

'################################################################
' Property
'################################################################
' 表名
Public Property Get Name() As String
    Name = tempName
End Property

' 表字段集合
' List<SelectItem>
Public Property Get SelectItems() As Collection
    Set SelectItems = fieldSelectItems
End Property

' 表项（From + Join）
' List<IFromItem>
Public Property Get FromItems() As Collection
    Set FromItems = froms
End Property

' where 条件的封装
' List<SqlCondition>
Public Property Get WhereConditions() As Collection
    Set WhereConditions = where.Conditions
End Property

' 分组条件的封装
' List<GroupbyItem>
Public Property Get GroupbyItems() As Collection
    Set GroupbyItems = groupbys
End Property

' 排序条件的封装
' List<OrderbyItem>
Public Property Get OrderbyItems() As Collection
    Set OrderbyItems = orderbys
End Property

'################################################################
' Implements ISql
'################################################################
Public Property Get ISql_SqlType() As SqlEnum_SqlType
    ISql_SqlType = SqlEnum_SqlType.TYPE_SELECT
End Property

'################################################################
' Implements ISqlTableDefine
'################################################################
' 获取表名
' 对于一个查询/子查询，没有真实的表名，只能返回一个临时表名
Public Function ISqlTableDefine_getTableName() As String
    ISqlTableDefine_getTableName = tempName
End Function

' 是否只使用了一个表
Public Function ISqlTableDefine_useSingleTable() As Boolean
    If froms.Count = 1 Then
        ISqlTableDefine_useSingleTable = True
    Else
        ISqlTableDefine_useSingleTable = False
    End If
End Function

' 检查是否使用表中的所有列
' Public Function ISqlTableDefine_useAllColumn() As Boolean
' End Function

' 获取所有列对象
Public Function ISqlTableDefine_getColumns() As Collection
    Set ISqlTableDefine_getColumns = fieldSelectItems
End Function

' 获取所有的筛选条件
Public Function ISqlTableDefine_getCondition() As Collection
    Set ISqlTableDefine_getCondition = where.Conditions
End Function

' 添加筛选条件
Public Sub ISqlTableDefine_addCondition(ByRef condition As SqlCondition)
    Me.addWhereCondition condition
End Sub

'################################################################
' Implements ISubSelectContainer
'################################################################
Public Function ISubSelectContainer_hasSubSelect() As Boolean
    ' 1. if subselect in froms
    ' 2. if subselect in where
End Function

Public Function ISubSelectContainer_getSubSelects() As Collection
    ' 1. if subselect in froms
    ' 2. if subselect in where
End Function

'################################################################
' Function
'################################################################
' 添加数据项
Public Sub addSelectItem(ByRef i As SelectItem)
    fieldSelectItems.Add i
End Sub

' 添加数据表
Public Sub addFrom(ByRef f As IFromItem)
    froms.Add f
End Sub

' 添加 where 条件
Public Sub addWhereCondition(ByRef c As SqlCondition)
    where.addCondition c
End Sub

' 添加分组字段
Public Sub addGroupby(ByRef g As GroupbyItem)
    groupbys.Add g
End Sub

' 添加排序字段
Public Sub addOrderby(ByRef o As OrderbyItem)
    orderbys.Add o
End Sub